#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <cstring>
#include <memory>
#include "Log.hpp"
#include "INetAddr.hpp"

namespace socket_ns
{
    const static int gBackLog = 8;
    class Socket;
    using socket_sptr = std::shared_ptr<Socket>;
    enum
    {
        SOCKET_ERROR = 1,
        BIND_ERROR,
        LISTEN_ERROR,
        CONNECT_ERROR
    };
    class Socket
    {
    public:
        virtual void CreateSocketOrDie() = 0;
        virtual void BindSocketOrDie(const INetAddr &addr) = 0;
        virtual void ListenSocketOrDie() = 0;
        virtual socket_sptr Acceptor(INetAddr &addr) = 0;
        virtual bool Connector(const INetAddr &addr) = 0;
        virtual int Socketfd() = 0;
        virtual void SetAddrReUse() = 0;
        virtual int Recv(std::string &in) = 0;
        virtual int Send(const std::string &out) = 0;
        virtual void Close() = 0;

    public:
        void BuildListenServer(const INetAddr &addr)
        {
            CreateSocketOrDie();
            SetAddrReUse();
            BindSocketOrDie(addr);
            ListenSocketOrDie();
        }
        bool BuildClient(const INetAddr &addr)
        {
            CreateSocketOrDie();
            return Connector(addr);
        }
    };

    class TcpSocket : public Socket
    {
    public:
        TcpSocket(int fd = -1)
            : _socketfd(fd)
        {
        }
        void CreateSocketOrDie() override
        {
            // 1. 创建套接字
            _socketfd = ::socket(AF_INET, SOCK_STREAM, 0);
            if (_socketfd < 0)
            {
                LOG(FATAL, "socket error\n");
                exit(SOCKET_ERROR);
            }
            LOG(INFO, "socket create success, sockfd: %d\n", _socketfd);
        }
        void BindSocketOrDie(const INetAddr &addr) override
        {
            struct sockaddr_in local = addr.Addr();
            int n = ::bind(_socketfd, (const sockaddr *)&local, sizeof local);
            if (n < 0)
            {
                LOG(FATAL, "bind error\n");
                exit(BIND_ERROR);
            }
            LOG(INFO, "bind success\n");
        }

        void ListenSocketOrDie() override
        {
            int n = ::listen(_socketfd, gBackLog);
            if (n < 0)
            {
                LOG(FATAL, "listen error\n");
                exit(LISTEN_ERROR);
            }
            LOG(INFO, "listen success\n");
        }

        socket_sptr Acceptor(INetAddr &addr) override
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof peer;
            int socket = ::accept(_socketfd, (sockaddr *)&peer, &len);
            if (socket < 0)
            {
                LOG(WARNING, "accept fail\n");
                return nullptr;
            }
            addr = peer;
            socket_sptr sptr = std::make_shared<TcpSocket>(socket);
            return sptr;
        }
        bool Connector(const INetAddr &addr) override
        {
            struct sockaddr_in peer = addr.Addr();
            int n = ::connect(_socketfd, (const sockaddr *)&peer, sizeof peer);
            if (n < 0)
            {
                LOG(ERROR, "connect fail\n");
                return false;
            }
            return true;
        }
        int Socketfd() override
        {
            return _socketfd;
        }
        int Recv(std::string &in) override
        {
            char inbuff[4096];
            ssize_t n = recv(_socketfd, inbuff, sizeof(inbuff) - 1, 0);
            if(n <= 0)
                return 0;
            inbuff[n] = '\0';
            in = inbuff;
            return n;
        }
        int Send(const std::string &out) override
        {
            int n =send(_socketfd, out.c_str(), out.size(), 0);
            return n;
        }

        void Close() override
        {
            ::close(_socketfd);
        }
        void SetAddrReUse() override
        {
            int opt = 1;
            setsockopt(_socketfd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof opt);
        }


    private:
        int _socketfd;
    };

};